package com.zhaoyang.dao;

import com.zhaoyang.model.Page;
import com.zhaoyang.model.Teacher;
import com.zhaoyang.util.StringUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 赵扬
 * @time 2020/4/27 16:42
 */
public class TeacherDao extends BaseDao {

    /**
     * 添加教师
     *
     * @param teacher
     * @return
     */
    public boolean addTeacher(Teacher teacher) {
        String sql = "insert into s_teacher values(?,?,?,sha(?),?,?,?,?,null)";
        int cnt = template.update(sql, teacher.getId(), teacher.getSn(), teacher.getName(),
                teacher.getPassword(), teacher.getClazzId(), teacher.getSex(),
                teacher.getMobile(), teacher.getQQ());
        return cnt == 1;
    }

    /**
     * 修改教师资料
     *
     * @param teacher
     * @return
     */
    public boolean editTeacher(Teacher teacher) {
        String sql = "update s_teacher set name = ?,sex = ?,mobile = ?,qq = ?,clazz_id = ? where id = ?";
        int cnt = template.update(sql, teacher.getName(), teacher.getSex(), teacher.getMobile(),
                teacher.getQQ(), teacher.getClazzId(), teacher.getId());

        return cnt == 1;
    }

    /**
     * 修改教师头像
     *
     * @param teacher
     * @return
     */
    public boolean setTeacherPhoto(Teacher teacher) {
        String sql = "update s_teacher set photo = ? where id = ?";
        int cnt = template.update(sql, teacher.getPhoto(), teacher.getId());

        return cnt > 0;
    }

    /**
     * 删除教师信息
     *
     * @param ids
     * @return
     */
    public boolean deleteTeacher(String[] ids) {
        if (0 == ids.length) {
            return true;
        }
        final int len = ids.length;
        StringBuilder sql = new StringBuilder("delete from s_teacher where id in(");
        for (int i = 0; i < len; i++) {
            if (i == len - 1) {
                sql.append("?)");
            } else {
                sql.append("?,");
            }
        }
        int cnt = template.update(sql.toString(), (Object) ids);
        return cnt == len;
    }

    /**
     * 根据教师id获取教师信息
     *
     * @param id
     * @return
     */
    public Teacher getTeacher(int id) {
        String sql = "select * from s_teacher where id = ?";
        List<Teacher> teachers = template.query(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class), id);

        if (0 == teachers.size()) {
            return null;
        }
        return teachers.get(0);
    }

    /**
     * @param teacher
     * @param page
     * @return
     */
    public List<Teacher> getTeacherList(Teacher teacher, Page page) {
        List<Teacher> ret = new ArrayList<Teacher>();
        String sql = "select * from s_teacher ";
        if (!StringUtil.isEmpty(teacher.getName())) {
            sql += "and name like '%" + teacher.getName() + "%'";
        }
        if (teacher.getClazzId() != 0) {
            sql += " and clazz_id = " + teacher.getClazzId();
        }
        if (teacher.getId() != 0) {
            sql += " and id = " + teacher.getId();
        }
        sql += " limit " + page.getStart() + "," + page.getPageSize();
        ResultSet resultSet = query(sql.replaceFirst("and", "where"));
        try {
            while (resultSet.next()) {
                Teacher t = new Teacher();
                t.setId(resultSet.getInt("id"));
                t.setClazzId(resultSet.getInt("clazz_id"));
                t.setMobile(resultSet.getString("mobile"));
                t.setName(resultSet.getString("name"));
                t.setPassword(resultSet.getString("password"));
                t.setPhoto(resultSet.getBinaryStream("photo"));
                t.setQQ(resultSet.getString("qq"));
                t.setSex(resultSet.getString("sex"));
                t.setSn(resultSet.getString("sn"));
                ret.add(t);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * @param teacher
     * @return
     */
    public int getTeacherListTotal(Teacher teacher) {
        int total = 0;
        String sql = "select count(*)as total from s_teacher ";
        if (!StringUtil.isEmpty(teacher.getName())) {
            sql += "and name like '%" + teacher.getName() + "%'";
        }
        if (teacher.getClazzId() != 0) {
            sql += " and clazz_id = " + teacher.getClazzId();
        }
        if (teacher.getId() != 0) {
            sql += " and id = " + teacher.getId();
        }
        ResultSet resultSet = query(sql.replaceFirst("and", "where"));
        try {
            while (resultSet.next()) {
                total = resultSet.getInt("total");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return total;
    }

    /**
     * 教师登录
     *
     * @param name
     * @param password
     * @return
     */
    public Teacher login(String name, String password) {
        String sql = "select * from s_teacher where name = '" + name + "' and password = '" + password + "'";
        ResultSet resultSet = query(sql);
        System.out.println(sql);
        if(resultSet != null) {
            try {
                if (resultSet.next()) {
                    Teacher teacher = new Teacher();
                    teacher.setId(resultSet.getInt("id"));
                    teacher.setName(resultSet.getString("name"));
                    teacher.setPassword(resultSet.getString("password"));
                    teacher.setClazzId(resultSet.getInt("clazz_id"));
                    teacher.setMobile(resultSet.getString("mobile"));
                    teacher.setPhoto(resultSet.getBinaryStream("photo"));
                    teacher.setQQ(resultSet.getString("qq"));
                    teacher.setSex(resultSet.getString("sex"));
                    teacher.setSn(resultSet.getString("sn"));
                    return teacher;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 修改教师密码
     *
     * @param teacher
     * @param newPassword
     * @return
     */
    public boolean editPassword(Teacher teacher, String newPassword) {
        String sql = "update s_teacher set password = sha(?) where id = ?";
        int cnt = template.update(sql, newPassword, teacher.getId());
        return cnt == 1;
    }

}
